package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.view.View
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.HomeFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.*
import com.ewzj.bigdata.mvp.ui.adapter.HomePageAdapter
import com.ewzj.bigdata.mvp.ui.adapter.HourseTypeAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.utils.DataHelper
import com.jess.arms.utils.DeviceUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import java.util.*
import javax.inject.Inject
import kotlin.collections.ArrayList

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class HomeFragmentPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: HomeFragmentContract.View, model: HomeFragmentContract.Model) : BasePresenter<HomeFragmentContract.Model, HomeFragmentContract.View>(model, view) {
    val datas = arrayListOf<HomeBean>()
    val adapter: HomePageAdapter by lazy { HomePageAdapter(datas) }
    var renthomedata: List<List<HomeBean>> = ArrayList<List<HomeBean>>()
    var rechomedata: List<List<HomeBean>> = ArrayList<List<HomeBean>>()
    var newhomedata: List<List<HomeBean>> = ArrayList<List<HomeBean>>()
    var oldhomedata: List<List<HomeBean>> =ArrayList<List<HomeBean>>()
    var rspHomedata: RspHomeData? = null
    private var recpage = 0
    private var newpage = 0
    private var oldpage = 0
    private var rendpage = 0
    var locationcity: String = ""
    private var hourseBeanList: MutableList<HomeBean> = ArrayList<HomeBean>()
    fun initHeaderview(headerview: View) {
        adapter.removeAllHeaderView()
        if (adapter.headerLayoutCount == 0) {
            adapter.addHeaderView(headerview)
        }
        adapter.refreshdataListener = object : HomePageAdapter.RefreshdataListener {
            override fun refreshdata(string: String) {
                when (string) {
                    "为您推荐" -> {
                        recpage++
                        if (recpage == rechomedata.size) {
                            recpage = 0
                        }
                        changeData()
                    }
                    "二手房" -> {
                        oldpage++
                        if (oldpage == oldhomedata.size) {
                            oldpage = 0
                        }
                        changeData()
                    }
                    "新房" -> {
                        newpage++
                        if (newpage == newhomedata.size) {
                            newpage = 0
                        }
                        changeData()
                    }
                    "租房" -> {
                        rendpage++
                        if (rendpage == renthomedata.size) {
                            rendpage = 0
                        }
                        changeData()
                    }
                }
            }

        }
        mRootView.initHomeRecylerveiew(adapter)
    }

    fun inithourseType() {
        val types: List<HourseType> = listOf(HourseType("2", "二手房", "1"), HourseType("1", "新房", "2"), HourseType("4", "写字楼", "3")
                , HourseType("5", "商铺", "4"), HourseType("0", "地图筛选", "5"))
        mRootView.initTypeRecylerveiew(HourseTypeAdapter(types))
    }

    fun initListdata() {
        val page: Reqpagebean = Reqpagebean()
        mModel.getHomev3(page).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.bindToLifecycle(mRootView))
                .subscribe(object : ErrorHandleSubscriber<RspHomeData>(mErrorHandler) {
                    override fun onNext(t: RspHomeData?) {
                        if (t != null) {
                            rspHomedata = t
                            mRootView.initHomeNumber(t)
                            makeData(t)
                        }
                    }

                })


    }

    fun newHomedatas(){
        val page: Reqpagebean = Reqpagebean()
        mModel.gerNewHomelist(page).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<RspHomeData>(mErrorHandler){
                    override fun onNext(rspHomeData: RspHomeData) {
                        newhomedata = mModel.subList<HomeBean>(rspHomeData.getXinfang())
                        hourseBeanList.add(HomeBean("新房"))
                        hourseBeanList.addAll(newhomedata[0])
                        adapter.setNewData(hourseBeanList)
                    }

                })
    }
    fun recHomedatas(){
        val page: Reqpagebean = Reqpagebean()
        mModel.gerRecHomelist(page).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<RspHomeData>(mErrorHandler){
                    override fun onNext(rspHomeData: RspHomeData) {
                        rechomedata = mModel.subList<HomeBean>(rspHomeData.tuijian)
                        hourseBeanList.add(HomeBean("为您推荐"))
                        hourseBeanList.addAll(rechomedata[0])
                        adapter.setNewData(hourseBeanList)
                    }

                })
    }
    fun oldHomedatas(){
        val page: Reqpagebean = Reqpagebean()
        mModel.gerOldHomelist(page).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<RspHomeData>(mErrorHandler){
                    override fun onNext(rspHomeData: RspHomeData) {
                        oldhomedata = mModel.subList<HomeBean>(rspHomeData.ershoufang)
                        hourseBeanList.add(HomeBean("二手房"))
                        hourseBeanList.addAll(oldhomedata[0])
                        adapter.setNewData(hourseBeanList)
                    }

                })
    }
    fun rentHomedatas(){
        val page: Reqpagebean = Reqpagebean()
        mModel.gerRentHomelist(page).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<RspHomeData>(mErrorHandler){
                    override fun onNext(rspHomeData: RspHomeData) {
                        renthomedata = mModel.subList<HomeBean>(rspHomeData.zufang)
                        hourseBeanList.add(HomeBean("租房"))
                        hourseBeanList.addAll(renthomedata[0])
                        adapter.setNewData(hourseBeanList)
                    }

                })
    }

    //   组装分割数据
    private fun makeData(rspHomeData: RspHomeData) {
        rechomedata = mModel.subList<HomeBean>(rspHomeData.getTuijian())
        newhomedata = mModel.subList<HomeBean>(rspHomeData.getXinfang())
        oldhomedata = mModel.subList<HomeBean>(rspHomeData.getErshoufang())
        renthomedata = mModel.subList<HomeBean>(rspHomeData.getZufang())
        initFirstpagedata()
    }

    //    组装第一页的数据
    private fun initFirstpagedata() {
        if (rechomedata.isNotEmpty()&&oldhomedata.isNotEmpty()&&newhomedata.isNotEmpty()&&renthomedata.isNotEmpty()){
            hourseBeanList.clear()
            hourseBeanList.add(HomeBean("为您推荐"))
            hourseBeanList.addAll(rechomedata[0])
            hourseBeanList.add(HomeBean("二手房"))
            hourseBeanList.addAll(oldhomedata[0])
            hourseBeanList.add(HomeBean("新房"))
            hourseBeanList.addAll(newhomedata[0])
            hourseBeanList.add(HomeBean("租房"))
            hourseBeanList.addAll(renthomedata[0])
            datas.clear()
            datas.addAll(hourseBeanList)
            adapter.notifyDataSetChanged()
        }
    }

    private fun changeData() {
        //       private int recpage = 0, newpage = 0, oldpage = 0, rendpage = 0;
        //     维护当前页码序列的坐标
        hourseBeanList.clear()
        hourseBeanList.add(HomeBean("为您推荐"))
        hourseBeanList.addAll(rechomedata[recpage])
        hourseBeanList.add(HomeBean("二手房"))
        hourseBeanList.addAll(oldhomedata[oldpage])
        hourseBeanList.add(HomeBean("新房"))
        hourseBeanList.addAll(newhomedata[newpage])
        hourseBeanList.add(HomeBean("租房"))
        hourseBeanList.addAll(renthomedata[rendpage])
        adapter.data.clear()
        adapter.data.addAll(hourseBeanList);
        adapter.notifyDataSetChanged()
    }

    fun onResume() {
        var spcity = DataHelper.getStringSF(application, AppKey.LOCATION_CITY)
        if (spcity.isNullOrEmpty()) return
        if (!locationcity.equals(spcity)) {
            var citybean = mGson.fromJson(spcity, CityBean.ListEntity::class.java)
            mRootView.locationCity(citybean.city,false)
            initListdata()
            getAdData()
//            newHomedatas()
//            rentHomedatas()
//            recHomedatas()
//            oldHomedatas()
            getTopicEnter()
            locationcity = DataHelper.getStringSF(application, AppKey.LOCATION_CITY)
        }
    }


    fun getAdData() {
        mModel.getAdList().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseData<AdBean>>(mErrorHandler) {
                    override fun onNext(t: BaseData<AdBean>) {
                        mRootView.initbanner(t.data!!)
                    }

                })
    }

    fun getpopAddata(){
        mModel.getpopAd().compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseData<AdBean>>(mErrorHandler) {
                    override fun onNext(t: BaseData<AdBean>) {
                       if (t.data!!.list!=null&&t.data!!.list.size>0){
                           if (t.data!!.list[0].images!=null&&t.data!!.list[0].images.size>0){
                               mRootView.showPopAdview(t.data!!)
                           }
                       }
                    }

                })

    }
    fun getTopicEnter(){
        mModel.gettopicEnter().compose(RxUtils.applySchedulers(mRootView))
                .subscribe({
                    t->run{
                    if (t.data!!.list!==null&&t.data!!.list!!.size>=2){
                        mRootView.setTopicEnter(t.data!!,true)
                    }else{
                        mRootView.setTopicEnter(t.data!!,false)
                    }
                }
                },{
                   t->t.printStackTrace()
                })
    }
}
